package com.zk.nakedfowl.basicData.controller;
import java.util.List;
import java.util.Map;

import com.zk.nakedfowl.basicData.pojo.Terminal;
import com.zk.nakedfowl.basicData.service.TerminalService;
import com.zk.nakedfowl.common.entity.PageResult;
import com.zk.nakedfowl.common.entity.Result;
import com.zk.nakedfowl.common.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/terminal")
public class TerminalController {

	@Autowired
	private TerminalService terminalService;
	
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true, StatusCode.OK,"查询成功",terminalService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id,String role,String createBy){
		if(role.equals("admin"))
			return new Result(true,StatusCode.OK,"查询成功",terminalService.findById(id));
		else if(role.equals("butcher")||role.equals("staller"))
			return new Result(true,StatusCode.OK,"查询成功",terminalService.findByIdName(id,createBy));
		else
			return new Result(false,StatusCode.ACCESSERROR,"你没有权限");

	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		String role=(String) searchMap.get("role");

		if(role.equals("admin")) {
			//查询全部，不需要根据createBy
			searchMap.remove("createBy");
		}

		Page<Terminal> pageList = terminalService.findSearch(searchMap, page, size);


		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Terminal>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){

		String role=(String) searchMap.get("role");

		if(role.equals("admin")) {
			//查询全部，不需要根据createBy
			searchMap.remove("createBy");
		}
    	return new Result(true,StatusCode.OK,"查询成功",terminalService.findSearch(searchMap));
    }
	
	/**
	 * 增加
	 * @param terminal
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Terminal terminal  ){
		terminalService.add(terminal);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param terminal
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Terminal terminal, @PathVariable String id ){
		terminal.setId(id);
		terminalService.update(terminal);		
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		terminalService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}
	
}
